package com.ls.fw.data.exp;

import java.util.Arrays;

import com.ls.fw.data.visitor.ExpressionVisitor;


/**
 * 比较表达式
 * @author ls
 * 2014年12月16日下午3:17:47
 */
public class Comparison extends Expression {
	
	protected String name = "";

	private Operator operator = Operator.EQ;

	protected Object value = null;
	
	protected Object value1 = null;

	protected float boost = 1.0F;
	
	
    /**
     * No args constructor.
     */
    public Comparison() {
    }
 
    
    public Comparison(String name, Operator operator, Object value,
			Object value1) {
		super();
		this.name = name;
		this.operator = operator;
		this.value = value;
		this.value1 = value1;
	}



	public Comparison(String name, Operator operator, Object value) {
		this(name, operator, value, null);
	}


	public Comparison(String name, Object value) {
		this(name, Operator.EQ, value, null);
	}


	/**
     * The operator.
     *
     * @return the operator
     */
    public Operator getOperator() {
        return operator;
    }

   

    /**
     * The enum for specifying the function we're performing in a {@link
     * Comparison}.
     */
    public static enum Operator {
          EQ("=","等于")
		, EQ1("==","等于")
		, GT(">","大于")
		, LT("<","小于")
		, GTE(">=","大于等于")
		, LTE("<=","小于等于")
		, NEQ("!=","不等于")
		, N("<>","不等于")
		, LIKE("like","等于")
		, NLIKE("not like","NOT LIKE")
		, PRE_LIKE("pre_like")
		, LAST_LIKE("last_like")
		, IS("is","等于")
		, ISN("is not","IS NOT")
		, IN("in","IN")
		, NIN("not in","NOT IN")
		, BETWEEN("between","BETWEEN")
		, NBETWEEN("not between","NOT BETWEEN")
		,IS_NULL("is null")
		,IS_NOT_NULL("is not null")
		,IS_EMPTY("is empty")
		,IS_NOT_EMPTY("is not empty")
		,NOT_CHAR("not ?")
		,CHAR("?")
		,PRE_CHAR("pre_?")
		,LAST_CHAR("last_?")
		, UNKOWN("","未知");
		
		private String name = "";
		private String desc = "";
		
		private Operator(){
			
		}
		private Operator(String name){
			this.name = name;
		}
		private Operator(String name,String desc){
			this.name = name;
			this.desc = desc;
		}
		public String getName() {
			return name;
		}
		public String getDesc() {
			return desc;
		}
		public static Operator get(String name){
			Operator[] opears = values();
			for (Operator opear : opears) {
				if(opear.getName().equalsIgnoreCase(name)){
					if(opear==EQ1){
						return Operator.EQ;
					}
					return opear;
				}
			}
			return UNKOWN;
		}
    }



	public String getName() {
		return name;
	}

	public Comparison setName(String name) {
		this.name = name;
		return this;
	}

	public Object getValue() {
		return value;
	}

	public Comparison setValue(Object value) {
		this.value = value;
		return this;
	}

	public Object getValue1() {
		return value1;
	}

	public Comparison setValue1(Object value1) {
		this.value1 = value1;
		return this;
	}

	public Comparison setOperator(Operator operator) {
		this.operator = operator;
		return this;
	}
	
	public Comparison setOperator(String operator) {
		this.operator = Operator.get(operator);
		return this;
	}
	
	@Override
	public String toString() {
		if (value instanceof Object[]) {
			return this.name + " " + this.operator.getName() + " " + Arrays.toString((Object[]) value);
		} else if (this.operator == Operator.BETWEEN || this.operator == Operator.NBETWEEN) {
			return this.name + " " + this.operator.getName() + " " + this.value+" "+this.value1;
		}else {
			return this.name + " " + this.operator.getName() + " " + this.value;
		}
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((operator == null) ? 0 : operator.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		result = prime * result + ((value1 == null) ? 0 : value1.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Comparison other = (Comparison) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (operator != other.operator)
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		if (value1 == null) {
			if (other.value1 != null)
				return false;
		} else if (!value1.equals(other.value1))
			return false;
		return true;
	}


	@Override
	public void accept(ExpressionVisitor visitor) {
		visitor.visit(this);
	}


	public float getBoost() {
		return this.boost;
	}


	public Comparison setBoost(float boost) {
		this.boost = boost;
		return this;
	}
}
